package com.huobao.yunying.activitys;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.xutils.view.annotation.ViewInject;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.huobao.yunying.base.HuobaoBaseActivity;
import com.huobao.yunying.entity.Regions;
import com.huobao.yunying.https.Http;
import com.huobao.yunying.https.HttpListener;
import com.huobao.yunying.https.HttpUtil;
import com.huobao.zhangying.R;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import joanzapata.android.BaseAdapterHelper;
import joanzapata.android.QuickAdapter;

public class Act_SelectArea extends HuobaoBaseActivity implements OnItemClickListener{

	@ViewInject(R.id.listview)
	private ListView listview;

	private List<Regions> m_AllRegions = new ArrayList<Regions>();

	private ContentAdapter adapter;
	private boolean selectSheng = true;
	private String selectname;
	@Override
	protected int getContentView() {
		return R.layout.layout_selecrarea;
	}

	@Override
	protected String getTitleText() {
		return "选择地区";
	}

	@Override
	protected void init() {
		adapter = new ContentAdapter(this);
		listview.setOnItemClickListener(this);
		listview.setAdapter(adapter);
		listview.setFastScrollEnabled(true); // 设置快速滑动
		getRegions();
	}

	private void getRegions() {
		Map<String, String> map = HttpUtil.getParams("app", "regions");
		Http.getInstant().sendRequest(context, map, new HttpListener() {

			@Override
			public void onHttpSuccess(String result) {
				JSONObject jsonObject = JSON.parseObject(result);
				JSONArray array = jsonObject.getJSONArray("retval");
				for (int i = 0; i < array.size(); i++) {
					JSONObject object = array.getJSONObject(i);
					Regions regions = new Regions();
					regions.region_id = object.getString("region_id");
					regions.region_name = object.getString("region_name");
					regions.children = JSON.parseArray(object.getString("children"), Regions.class);
					m_AllRegions.add(regions);
				}
				adapter.addAll(m_AllRegions);
			}

			@Override
			public void onHttpError(String error) {

			}
		});
	}

	private class ContentAdapter extends QuickAdapter<Regions>{

		public ContentAdapter(Context context) {
			super(context, android.R.layout.simple_list_item_1);
		}

		@Override
		protected void convert(BaseAdapterHelper arg0, Regions arg1) {	
			arg0.setText(android.R.id.text1, arg1.region_name);
		}
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		if(selectSheng){
			List<Regions> list = m_AllRegions.get(position).children;
			if(list == null || list.size() == 0){
				setResultData(m_AllRegions.get(position).region_id, m_AllRegions.get(position).region_name);
			}else{
				adapter.clear();
				adapter.addAll(list);
				selectSheng = false;
				selectname = m_AllRegions.get(position).region_name;
			}
		}else{
			selectname = selectname + "  "+ adapter.getItem(position).region_name;
			setResultData(adapter.getItem(position).region_id, selectname);
		}
	}
	
	private void setResultData(String id, String name){
		Intent intent = new Intent();
		intent.putExtra("id", id);
		intent.putExtra("name", name);
		setResult(RESULT_OK, intent);
		finish();
	}

}
